package com.tianfeng.huayi.controller;

import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.tianfeng.huayi.entity.Owner;
import com.tianfeng.huayi.entity.Property;
import com.tianfeng.huayi.service.IOwnerService;
import com.tianfeng.huayi.util.CaptchaGenerator;
import com.tianfeng.huayi.util.Sample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/owner")
@CrossOrigin
public class OwnerController {

    @Autowired
    private IOwnerService ownerService;

    private String confirmCaptcha;

    /**
     * 根据业主ID查询业主信息
     */
    /**
     * 根据房产ID查询房产信息列表
     */
    @PostMapping("/selectOwnerByOwnerId")
    @ResponseBody
    public ResponseEntity<Map<Object, Object>> selectOwnerByOwnerId(@RequestBody Owner owner)
    {
        Map<Object,Object> response = new HashMap<>();
        Owner selected = ownerService.selectOwnerByOwnerId(owner.getOwnerId());
        if (selected != null) {
            response.put("code", 200);
            response.put("msg", "查询成功");
            response.put("data", selected);
        } else {
            response.put("code", 400);
            response.put("msg", "查询失败");
        }
        return ResponseEntity.ok(response);
    }

    /**
     小程序登录发送验证码
     */
    @PostMapping("/weapplogincaptcha")
    public ResponseEntity<Map<Object, Object>> weAppLoginCaptcha(@RequestBody Owner owner) throws Exception {
        Map<Object,Object> response = new HashMap<>();
        System.out.println("发送验证码");
        List<Owner> owners = ownerService.selectOwnerList(owner);
        if (!owners.isEmpty()) {
            // 生成6位数字验证码
            String captcha = CaptchaGenerator.generateCaptcha();
            confirmCaptcha = captcha;
            Integer smsResponse = Sample.LoginCaptcha(owner.getContactInfo(), captcha);
            System.out.println(smsResponse);

            if (smsResponse == 200) {
                response.put("code", 200);
                response.put("msg", "验证码发送成功");
                response.put("data", captcha);
            } else {
                response.put("code", 400);
                response.put("msg", "验证码发送失败");
            }
        } else {
            response.put("code", 400);
            response.put("msg", "该手机号不是业主");
        }
        return ResponseEntity.ok(response);
    }

    /**
     小程序登录
     */
    @PostMapping("/weapplogin")
    public ResponseEntity<Map<Object, Object>> weAppLogin(@RequestBody Map<String, String> requestData) {
        Map<Object,Object> response = new HashMap<>();
        String contactInfo = requestData.get("contactInfo");
        String captcha = requestData.get("captcha");
        Owner owner = new Owner();
        owner.setContactInfo(contactInfo);
        List<Owner> owners = ownerService.selectOwnerList(owner);
        if (!Objects.equals(confirmCaptcha, "")) {
            if (!owners.isEmpty() && captcha.equals(confirmCaptcha)) {
                response.put("code", 200);
                response.put("msg", "登录成功");
                response.put("data", owners.get(0));
                confirmCaptcha = "";
            } else {
                response.put("code", 400);
                response.put("msg", "验证码错误");
            }
        } else {
            response.put("code", 400);
            response.put("msg", "验证码已失效");
        }
        return ResponseEntity.ok(response);
    }


}
